package models

import (
	"gorm.io/gorm"
)

type Order struct {
	gorm.Model
	UserId      int     `gorm:"type:int;not null;index;comment:用户id"`
	OrderSn     string  `gorm:"type:char(36);not null;index;comment:订单号"`
	PayType     int     `gorm:"type:tinyint(1);default:1;comment:支付类型 1-支付宝 2-微信"`
	OrderStatus int     `gorm:"type:tinyint(1);default:1;comment:订单状态 1-待支付 2-已支付 3-已发货 4-已完成 5-已取消"`
	PayTime     int     `gorm:"type:int;default:0;comment:支付时间"`
	Amount      float64 `gorm:"type:decimal(10,2);default:0.00;comment:订单金额"`
}

// 订单商品表
type OrderGoods struct {
	gorm.Model
	OrderId    int     `gorm:"type:int;not null;index;comment:订单id"`
	GoodsId    int     `gorm:"type:int;not null;index;comment:商品id"`
	GoodsTitle string  `gorm:"type:varchar(100);not null;comment:商品标题"`
	GoodsCover string  `gorm:"type:varchar(255);not null;comment:商品封面"`
	GoodsPrice float64 `gorm:"type:decimal(10,2);not null;comment:商品价格"`
	GoodsNum   int     `gorm:"type:int;not null;comment:商品数量"`
}

// 创建订单
func (o *Order) Create() error {
	return DB.Create(&o).Error
}

// 创建订单商品
func (og *OrderGoods) Create() error {
	return DB.Create(&og).Error
}

// 修改订单状态
func (o *Order) Update(orderSn string, status int) error {
	return DB.Debug().Model(&o).Where("order_sn = ?", orderSn).Update("order_status", status).Error
}
